/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import Domain.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
/**
 *
 * @author Allen
 */
public class ObservationReader {
    public static List<Output[]> readOutputs(String filename) {
        File file = new File(filename);
        BufferedReader reader = null;
        List<Output[]> output_List = new ArrayList<Output[]>();

        int line = 0;
         try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                output_List.add(readOutput(tempString));
                line ++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            return output_List;
        }
    }
    private static Output[] readOutput(String str){
        int length = str.length();
        char inputs[] = str.toCharArray();
        Output[] temp = new Output[length/2 + 1];
        int i, j;
        for (i = 0, j = 0; i < length;){
            if(inputs[i] == ' ') {
                i ++;
                continue;
            }
            if(inputs[i] == 'c') {
                temp[j] = Output.toOutput(0);
            } else if (inputs[i] == 'e') {
                temp[j] = Output.toOutput(1);
            } else if (inputs[i] == 'r') {
                temp[j] = Output.toOutput(2);
            } else break;
            i ++;
            i ++;
            j ++;
        }

        Output[] result = new Output[j];
        for(i = 0; i < j; i ++) {
            result[i] = temp[i];
        }
        return result;
    }
}
